package com.liucanwen.citylist.hccitylist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.liucanwen.citylist.R;
import com.liucanwen.citylist.hccitylist.adapter.CityAdapter;
import com.liucanwen.citylist.hccitylist.adapter.SerchAdapter;
import com.liucanwen.citylist.hccitylist.cityinterface.City;
import com.liucanwen.citylist.hccitylist.view.TouchView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/9/1.
 */
public class ChooseCityActivity extends Activity implements View.OnClickListener {

//    private int[] arr_idex = new int[]{0, 1, 11 + 1, 32 + 1, 49 + 1, 64 + 1, 68 + 1, 76 + 1, 88 + 1, 124 + 1, 147 + 1, 155 + 1, 184 + 1, 191 + 1, 204 + 1, 213 + 1, 228 + 1, 231 + 1, 263 + 1, 281 + 1, 297 + 1, 321, 347};
    private int[] arr_idex = new int[]{0, 1, 11 + 1, 32 + 1, 49 + 1, 64 + 1, 68 + 1, 76 + 1, 88 + 1, 124 + 1, 147 + 1, 155 + 1, 184 + 1, 191 + 1, 204 + 1, 213 + 1, 228 + 1, 231 + 1, 263 , 281 , 297 , 321, 347};
    private String[] arr_idex_char = new String[]{"#","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z",};
    public static final int ChooseCityCode = 911;
    private EditText searchBox;
    private ArrayList<String> mCityList;
    private CityAdapter mCityAdapter;
    private SerchAdapter mSearchAdapter;
    private ListView mCityLv;
    private View mHeadView;
    private TextView mTv_center;

    /**
     * A 0+11
     * B 12-31
     * C 32-47
     * D 47-
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choosecity);
        City.getInstance().initList();
        //-----------------------ET
        EditText searchBox = (EditText) findViewById(R.id.input_search_query);
        searchBox.setFocusable(false);
        searchBox.setOnClickListener(this);
        mCityLv = (ListView) findViewById(R.id.lv_city);
        mCityList = City.getInstance().getCityList();
        mCityLv.addHeaderView(mHeadView = getHeadView());
        mCityLv.setAdapter(mCityAdapter = new CityAdapter(this, mCityList));
        TouchView touchview = (TouchView) findViewById(R.id.touchview);
        touchview.setOnSelectedPositionListener(new TouchView.OnSelectedPositionListener() {
            @Override
            public void onSelected(int position) {
                Log.e("positionAA"," position  "+position);
                position = position > (arr_idex.length - 1) ? arr_idex.length - 1 : position; // 当偶尔手指太底部的时候会导致整除取值多 1 所以加这个逻辑防止越界异常
                position = position < 0 ? 0:position;
                mCityLv.setSelection(arr_idex[position]);
                mTv_center.setText(arr_idex_char[position]);
            }

            @Override
            public void onTouchUp() {
                mTv_center.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTouchDwon() {
                mTv_center.setVisibility(View.VISIBLE);
            }
        });

        mCityLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position == 0)
                    return;
                String city = mCityList.get(position - 1);
                if (city.length() != 1)
                    afterSelected(city);
            }
        });

        mTv_center = (TextView) findViewById(R.id.tv_center);
        mTv_center.setBackgroundResource(R.drawable.shape_grayconner_whitefill_3conner);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
    }

    private View getHeadView() {
        View view = View.inflate(this, R.layout.headlayout, null);
        view.findViewById(R.id.tv_beijing).setOnClickListener(this);
        view.findViewById(R.id.tv_shanghai).setOnClickListener(this);
        view.findViewById(R.id.tv_guangzhou).setOnClickListener(this);
        view.findViewById(R.id.tv_shenzhen).setOnClickListener(this);
        view.findViewById(R.id.tv_chengdu).setOnClickListener(this);
        view.findViewById(R.id.tv_hangzhou).setOnClickListener(this);
        view.findViewById(R.id.tv_wuhan).setOnClickListener(this);
        view.findViewById(R.id.tv_suzhou).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        String SelectedCity = null;
        int i = view.getId();
        if (i == R.id.tv_beijing) {
            SelectedCity = "北京";
        } else if (i == R.id.tv_shanghai) {
            SelectedCity = "上海";
        } else if (i == R.id.tv_guangzhou) {
            SelectedCity = "广州";
        } else if (i == R.id.tv_shenzhen) {
            SelectedCity = "深圳";
        } else if (i == R.id.tv_chengdu) {
            SelectedCity = "成都";
        } else if (i == R.id.tv_hangzhou) {
            SelectedCity = "杭州";
        } else if (i == R.id.tv_wuhan) {
            SelectedCity = "武汉";
        } else if (i == R.id.tv_suzhou) {
            SelectedCity = "苏州";
        } else if(i == R.id.input_search_query){
            Intent intent = new Intent(this,SelectedCityActivity.class);
            startActivityForResult(intent,911);
            return;
        }
        afterSelected(SelectedCity);
    }

    private void afterSelected(String city) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("city", city);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent，当然也可以自己定义新的Bundle
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ( data!=null && requestCode == 911 ){
            afterSelected(data.getExtras().getString("city"));
        }
    }

    //
//
//    //-------------------------------------------头布局的显示隐藏
//    private void hideshowHeadView() {
//        if (inSearchMode)
//            mHeadView.setVisibility(View.GONE);
//        else
//            mHeadView.setVisibility(View.VISIBLE);
//    }

}
